import { isNumbar, isString, skipWhitespace } from "./utils";

export enum TokenType {
  LeftParen = "LeftParen",
  RightParen = "RightParen",

  Identifier = "Identifier",
  Number = "Number",
}

export interface Token {
  type: TokenType;
  value: string;
}

// 词法分析
export function tokenizer(code: string) {
  const tokens: Token[] = [];
  let current = 0;

  while (current < code.length) {
    let char = code[current];

    if (skipWhitespace(char)) {
      current++;
      continue;
    }

    if (char === "(") {
      tokens.push({
        type: TokenType.LeftParen,
        value: char,
      });
      current++;
      continue;
    }

    if (char === ")") {
      tokens.push({
        type: TokenType.RightParen,
        value: char,
      });
      current++;
      continue;
    }

    if (isString(char)) {
      let value = "";
      while (isString(char) && current < code.length) {
        value += char;
        char = code[++current];
      }

      tokens.push({ type: TokenType.Identifier, value });
    }

    if (isNumbar(char)) {
      let value = "";
      while (isNumbar(char) && current < code.length) {
        value += char;
        char = code[++current];
      }

      tokens.push({ type: TokenType.Number, value });
    }
  }

  return tokens;
}
